$(function () {

    var orderTable = function (list) {
        var str = '';
        for (var i = 0; i < list.length; i++) {

            str += '<tr data-id="' + list[i].id + '">' +
                '<td>' + list[i].orderId + '</td>' +
                '<td>' + list[i].nickName + '</td>' +
                '<td>' + list[i].commodity.title + '</td>' +
                '<td>' + list[i].currentCost + '</td>' +
                '<td>' + list[i].amount + '</td>' +
                '<td>' + list[i].costIndent + '</td>' +
                '<td>' + list[i].statusVal + '</td>' +
                '<td>' + list[i].expressStatusVal + '</td>' +
                '<td>' + new Date(list[i].createTime).format('yyyy-MM-dd hh:mm:ss') + '</td>' +
                '<td>' +
                '<a class="btn btn-primary btn-xs addr" data-extra="' + list[i].extra + '" data-addr="' + list[i].addrInfo + '">商品,收货地址查看</a> <a class="btn btn-danger btn-xs delete">删除</a></td> ' +
                '</tr>'
        }

        return str
    };

    var categoryPage = new PageCreate({
        url: '/order/doPageQuery',
        container: $("#table tbody"),
        pagination: $("#page"),
        success: function (container, data) {
            container.html(orderTable(data.list));
        }
    });

    var searchPage = function () {
        var $formQuery = $(".form-query");

        function queryDatum() {
            categoryPage.init({
                pageNum: 1,
                pageSize: 10,
                orderId: $formQuery.find('.form-control[name=orderId]').val() || "",
                status: $formQuery.find('.form-control[name=status]').val() || "",
                nickName: $formQuery.find('.form-control[name=nickName]').val() || "",
                expressStatus: $formQuery.find('.form-control[name=expressStatus]').val() || "",
            });
        }

        function resetDatum() {
            $formQuery.find('.form-control[name=orderId]').val("");
            $formQuery.find('.form-control[name=status]').val("");
            $formQuery.find('.form-control[name=nickName]').val("");
            $formQuery.find('.form-control[name=expressStatus]').val("");
            categoryPage.init({
                pageNum: 1,
                pageSize: 10
            })
        }

        $("#queryDatum").off("click").on("click", function () {
            queryDatum()
        });

        $("#resetDatum").off("click").on("click", function () {
            resetDatum()
        });
    }

    var pageRender = function (curr, list) {
        var curr = curr - 1
        var _curr = curr * 10

        $("#table tbody").html(orderTable(list.slice(_curr, _curr + 10)));
    }
    var init = function init() {
        /* categoryPage.init({
         pageNum: 1,
         pageSize: 10,
         token: window.localStorage.getItem('token')
         })*/

        var gcurr = 1
        var initPage = function () {
            $.restPost('/order/doQuery').done(function (mess, data) {
                var list = data.list;
                laypage({
                    cont: $("#page"), //容器。
                    pages: Math.ceil(list.length / 10), //通过后台拿到的总页数
                    curr: gcurr, //当前页
                    skip: true, //是否开启跳页
                    skin: 'molv', //皮肤,
                    jump: function (obj, first) { //触发分页后的回调
                        gcurr = obj.curr
                        pageRender(gcurr, list)
                    }
                });
            })
        }

        //查看地址信息
        $('#table tbody').on('click','.addr',function(){
            var addr = $(this).data("addr");
            var extra = $(this).data("extra");
            $.rootOpenDialog({
                title: "地址查看",
                btn: ["关闭"],
                content: "<div style='padding: 20px;'><p style='padding-bottom: 20px;;'>" + addr + "</p><p>" + extra + "</p></div>",
                yes:function(index, lay){
                    layer.close(index);
                }
            });
        });

        //删除
        $("#table tbody").on('click', '.delete', function () {
            var $this = $(this);
            layer.confirm("确定进行此操作吗？", function (index) {
                $.restPost('/order/doRemove/' + $this.parents('tr').data('id')).done(function (mess, data) {
                    $.rootSuccessDialog(mess, 1500);
                    initPage();
                }).fail(function (mess) {
                    $.rootErrorDialog(mess, 1500);
                });
            });

        });

        // //同意
        // $("#table tbody").on('click', '.agree', function () {
        //     var $this = $(this);
        //     layer.confirm("确定进行此操作吗？", function (index) {
        //         $.restPost('/order/doUpdate', {
        //             id: $this.parents('tr').data('id'),
        //             status: 1
        //         }).done(function (mess, data) {
        //             $.rootSuccessDialog(mess, 1500);
        //             initPage();
        //         }).fail(function (mess) {
        //             $.rootErrorDialog(mess, 1500);
        //         })
        //     });
        // });

        initPage();

        searchPage()
    }

    init()
});



